<?php
class Emp_Profile extends Public_Controller{
	public $profiles;
	public $cache_profile = 'emp_profile/profile_';
	function __construct(){
		parent::__construct();
		/*
		khi chạy vào đây sẽ lưu cache toàn bộ thông tin nhân viên vào cache -> có nên hay ko ? 
		Lưu từng file hay lưu 1 file ? 
		*/
		$this->load->model('profile_m');
		$this->load->library('kunkuncache');
		$file_cache = 'emp_profile/profiles';
		if(!$this->profiles = $this->kunkuncache->get($file_cache)) 
		{
			$param['active'] = 1;
			$this->profiles = $this->profile_m->get_all();
			$this->kunkuncache->write($this->profiles,$file_cache);
			
			$this->load->model('users/groups_m');
			$this->load->model('users/list_m');
			
			foreach($this->profiles as $item){ // lưu cache 1 nhan vien
				
				$groups = $this->groups_m->get_all();
				$groups_select = array_for_select($groups,'id','description');
				$item->group = return_single($groups_select,$item->group_id);				
				$sex_select = $this->list_m->sex_select;
				$item->sex = return_single($sex_select,$item->sex);
				$this->kunkuncache->write($item,$this->cache_profile.$item->id);
			}
		}
	}
	
	public function insert(){
		$json = array();
		$model = $this->input->post();
		$this->form_validation->set_rules('full_name', lang('global:title'), 'trim|required|max_length[30]');
		$this->form_validation->set_rules('content', lang('global:content'), 'trim|required|max_length[300]|min_length[6]');
		if ($this->form_validation->run() == TRUE)
		{
			$id_insert = $this->comment_m->insert($model);
			$json['id']  = $id_insert;
		}
		else{
			$json['error'] = validation_errors('<p class="error"> - ', '</p>');
		}
		
		echo json_encode($json);
	}
	
	//ajax
	public function info($id){
		$profile = $this->kunkuncache->get($this->cache_profile.$id);
		$this->template
			->set_layout(FALSE)
			->build($this->controller.'/info', array(
			'profile' => $profile
		));
	}
	public function background(){
		$color = $this->input->post('background');
		$this->profile_m->background($this->current_user->id,$color);
		$this->session->set_userdata('background',$color);
		//echo $this->session->userdata('background');
		
	}
	
	// ajax from /admin/users : update array
	public function update_salary(){
		$salary = $this->input->post('data');
		$this->profile_m->update_salary($salary);
	}
	public function update_salary2(){
		$salary2 = $this->input->post('data');
		$this->profile_m->update_salary2($salary2);
	}
	public function update_factor(){
		$salary = $this->input->post('data');
		$this->profile_m->update_factor($salary);
	}
	public function update_s1(){
		$salary = $this->input->post('data');
		$this->profile_m->update_s1($salary);
	}
	public function update_s2(){
		$salary = $this->input->post('data');
		$this->profile_m->update_s2($salary);
	}
	
	//tao ma nhan vien dua vao id (5 ky tu)
	public function create_id2(){
		$this->profile_m->create_id2();
	}
	
}